<?php
namespace My\Controller;

use Utuo\BackgroundController;

/**
 * 像册管理
 * @author song
 *
 */
class PhotoController extends BackgroundController {
	
	protected $pt_id; // 像册id
	
	public function _initialize() {
		$this->pt_id = $pt_id = I('grp_id'); // 像册id
		$this->setControllerTitle("像册管理");
		$this->setActionTitle(array(
			'index' => '像册列表',
			'add' => '添加像册',
			'modify' => '编辑像册',
			'recycle' => '回收站',
		));
		$this->setActionTitle('list', '图片列表', "grp_id={$pt_id}");
		$this->setActionTitle('imageRecycle', '图片回收站', "grp_id={$pt_id}");
		$this->setActionNav(false)->setActionNav("index,add,recycle");
	}
	
	private function resetHeader() {
		$this->setActionNav(false)->setActionNav("list,imageRecycle,index");
	}
	
	// 像册列表
	public function indexAction() {
		$this->getList(0, 'photo');
	}
	
	// 图片列表
	public function listAction() {
		$this->resetHeader();
		$photoImagesList = $this->getList(0, 'photoImages', 1, array('pt_id' => $this->pt_id));
		$this->assign("photoImagesJSON", json_encode($photoImagesList));
		$this->display();
	}
	
	// 添加图片
	public function addImageAction() {
		C('TOKEN_ON', true);
		$this->resetHeader();
		$this->display();
	}
	
	// 添加像册
	public function addImagePostAction() {
		C('TOKEN_ON', true);
		$images = I("post.images");
		$pt_id = I("post.grp_id");
		$images = htmlspecialchars_decode($images);
		$images = json_decode($images);
		$imagesList = array();
		foreach ($images as $v) {
			$imagesList[] = array('url' => $v->url, 'pt_id' => $pt_id, 'cid' => $this->aid);
		}
		$d = D("PhotoImages");
		if(empty($imagesList) or false !== $d->addAll($imagesList)){
			$this->ajaxSuccess();
		}else{
			$this->ajaxError($d->getError());
		}
	}
	
	// 添加像册
	public function addAction() {
		C('TOKEN_ON', true);
		$this->display();
	}
	
	// 编辑像册
	public function modifyAction() {
		$photo = $this->getOneById("Photo");
		$this->assign("photo", $photo);
		$this->display();
	}
	
	// 保存像册
	public function saveAction() {
		$dPhoto = D('Photo');
		$dPhoto->submit() === false && $this->ajaxError($dPhoto->getError());
		$this->clacImageCount($this->pt_id);
		$this->ajaxSuccess();
	}
	
	/**
	 * 获得列表
	 * @param number $is_deleted
	 */
	private function getList($is_deleted = 0, $model, $return = 0, $wparam = array()) {
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$d = D($model);
		$condition = array (
				'is_deleted' => $is_deleted,
				'cid' => $this->aid
		);
		if(!empty($wparam)) $condition = array_merge($condition, $wparam);
		$Lists = $d->where($condition)->limit($pagesize)->page($page)->order('id DESC')->select();
		$Count = $d->where($condition)->count();
		$this->assign("{$model}Lists", $Lists);
		$this->showPage($Count, $pagesize);
		$return or $this->display();
		if($return) return $Lists;
	}
	
	
	// 回收站
	public function recycleAction() {
		$this->getList(1, 'photo');
	}
	
	// 设置状态
	public function setStatusPostAction() {
		$this->ajaxSetStatus();
	}
	
	// 移除
	public function removePostAction() {
		$this->ajaxRemove();
	}
	
	// 恢复
	public function restorePostAction() {
		$this->ajaxRestore();
	}
	
	// 删除
	public function deletePostAction() {
		$id = I('id', 0, "intval");
		if(M("photoImages")->where(array('cid' => $this->aid, 'pt_id'=>$id))->count()) {
			//$this->ajaxError("未能删除像册，当前相册还有图片.");
			M("photoImages")->where(array('cid' => $this->aid, 'pt_id'=>$id))->delete();
		}
		$this->ajaxDelete();
	}
	
	// 图片回收站
	public function imageRecycleAction() {
		$this->resetHeader();
		$photoImagesList = $this->getList(1, 'photoImages', 1, array('pt_id' => $this->pt_id));
		$this->assign("photoImagesJSON", json_encode($photoImagesList));
		$this->display();
	}
	
	// 移除图片
	public function removeImagePostAction() {
		$this->addEventListener("afterRemove", function($e, $scope) { call_user_method("clacImageCount", $scope);  });
		$this->ajaxRemove('photoImages');
	}
	
	// 恢复图片
	public function restoreImagePostAction() {
		$this->addEventListener("afterRestore", function($e, $scope) { call_user_method("clacImageCount", $scope);	});
		$this->ajaxRestore('photoImages');
	}
	
	// 删除图片
	public function deleteImagePostAction() {
		$this->ajaxDelete('photoImages');
	}
	
	// 计算图片数量
	public function clacImageCount($ptId = 0) {
		if(!$ptId){
			$id = I('id', 0, 'intval');
			$housePhotoImage = M("HousePhotoImages")->find($id) and $ptId = $housePhotoImage['pt_id'];
		}
		$count = M("HousePhotoImages")->where(array('pt_id'=>$ptId, 'is_deleted'=>0))->count();
		M("HousePhoto")->where("id={$ptId}")->setField("count", $count);
	}
	
}